package com.dldata.devtools30.service.api;


import com.dldata.devtools30.entity.DevtApiTypeEntity;
import com.dldata.devtools30.repository.api.DevtApiTypeEntityDao;
import com.dldata.devtools30.utils.StringUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by ai on 2017/1/11.
 */
@Component
@Transactional
public class DevtApiTypeEntityService {
    @Autowired
    DevtApiTypeEntityDao devtApiTypeEntityDao;
    public List<DevtApiTypeEntity> getAllData() {
        return Lists.newArrayList(devtApiTypeEntityDao.findAll()).stream().sorted(
                new Comparator<DevtApiTypeEntity>() {
                    @Override
                    public int compare(DevtApiTypeEntity o1, DevtApiTypeEntity o2) {
                        if(StringUtil.isEmptyOrLength0(o1.getXh())||StringUtil.isEmptyOrLength0(o2.getXh())){
                                return 0;
                        }
                        return o1.getXh().compareTo(o2.getXh());
                    }
                }
        ).collect(Collectors.toList());
    }

    public DevtApiTypeEntity findOne(String id){
        return devtApiTypeEntityDao.findById(id).get();
    }

    public DevtApiTypeEntity save(DevtApiTypeEntity devtApiTypeEntity){
        return  devtApiTypeEntityDao.save(devtApiTypeEntity);
    }

    public void delete(String id){
        devtApiTypeEntityDao.deleteById(id);
    }

}
